package org.millioncall.yueyoga.admin.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.millioncall.openplatform.common.PaginatedList;
import org.millioncall.yueyoga.admin.dao.GoodsDAO;
import org.millioncall.yueyoga.admin.model.AviatorExpression;
import org.millioncall.yueyoga.admin.model.Goods;
import org.millioncall.yueyoga.admin.service.GoodsService;
import org.springframework.stereotype.Service;

@Service
public class GoodsServiceImpl implements GoodsService {

	private GoodsDAO goodsDAO;

	@Override
	public boolean save(Goods goods) {
		Map<String, Object> map = new HashMap<String, Object>();
		if (goods != null) {
			map.put("name", goods.getName());
			map.put("code", goodsDAO.getNextCode());
			map.put("type", goods.getType());
			map.put("counts", goods.getCounts());
			map.put("status", 0);
			map.put("createtime", new Date());
			map.put("remarks", goods.getRemarks());
			map.put("price", goods.getPrice());
			map.put("organizationId", goods.getOrganizationId());
			return goodsDAO.save(map) > 0;
		}
		return false;
	}

	@Override
	public boolean update(Goods goods) {
		Map<String, Object> map = new HashMap<String, Object>();
		if (goods != null) {
			if (StringUtils.isNotBlank(goods.getName())) {
				map.put("name", goods.getName());
			}
			if (goods.getCounts() != null) {
				map.put("counts", goods.getCounts());
			}
			if(goods.getCode()!=null){
				map.put("code", goods.getCode());
			}
			if (goods.getStatus() != null) {
				map.put("status", goods.getStatus());
			}
			if (goods.getType() != null) {
				map.put("type", goods.getType());
			}
			if (StringUtils.isNotBlank(goods.getRemarks())) {
				map.put("remarks", goods.getRemarks());
			}
			if (goods.getPrice() != null) {
				map.put("price", goods.getPrice());
			}
			if (goods.getOrganizationId() != null) {
				map.put("organizationId", goods.getOrganizationId());
			}
			map.put("id", goods.getId());
		}
		if (!map.isEmpty()) {
			return goodsDAO.update(map) > 0;
		}
		return false;
	}

	@Override
	public boolean deleteById(Integer id) {
		if (id != null) {
			return goodsDAO.deleteById(id) > 0;
		}
		return false;
	}

	@Override
	public boolean deleteByIds(String[] ids) {
		String[] strs = null;
		List<String> list = new ArrayList<String>();
		for (int i = 0; i < ids.length; i++) {
			if (!list.contains(ids[i])) {// 如果数组 list 不包含当前项，则增加该项到数组中
				list.add(ids[i]);
			}
		}
		strs = list.toArray(new String[1]);
		if (strs != null && strs.length > 0) {
			return goodsDAO.deleteByIds(strs) > 0;
		}
		return false;
	}

	@Override
	public Goods findGoodsById(Integer id) {
		if (id != null && id.intValue() > 0) {
			return goodsDAO.findGoodsById(id);
		}
		return null;
	}

	@Override
	public PaginatedList<Goods> findGoodsInPage(Map<String, Object> map,
			int start, int limit) {
		return goodsDAO.findGoodsInPage(map, start, limit);
	}

	public GoodsDAO getGoodsDAO() {
		return goodsDAO;
	}

	public void setGoodsDAO(GoodsDAO goodsDAO) {
		this.goodsDAO = goodsDAO;
	}

	@Override
	public List<Goods> findAll(Map<String, Object> map) {
		return goodsDAO.findAll(map);
	}

	@Override
	public Goods findGoodsByCode(String code) {
		return goodsDAO.findGoodsByCode(code);
	}

	@Override
	public boolean updateGoodCounts(Map<String, Object> context) {
		int i = goodsDAO.updateGoodCounts(context);
		if (i > 0) {
			return true;
		} else {
			return false;
		}
	}

	@Override
	public List<Goods> findAllByOrganizationId(String organizationId) {
		return goodsDAO.findAllByOrganizationId(organizationId);
	}

	@Override
	public List<Goods> getAllGoodsWithAviator(Map<String, Object> params,
			AviatorExpression expression, Map<String, Object> aviatorParams,
			int limit) {
		return this.goodsDAO.getAllGoodsWithAviator(params, expression, aviatorParams, limit);
	}

}
